package com.renwei.yunlong.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.LoginBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 企业信息--页面
 * <p>
 * Created by Administrator on 2017/8/25.
 */

public class EnterpriseInformationActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_edit)
    TextView tvEdit;
    @Bind(R.id.rl_certification)
    RelativeLayout rlCertification;//企业认证
    @Bind(R.id.rl_code)
    RelativeLayout rlCode;//企业二维码
    @Bind(R.id.rl_contact)
    RelativeLayout rlContact;//联系人
    @Bind(R.id.rl_phone)
    RelativeLayout rlPhone;//手机号
    @Bind(R.id.rl_documents)
    RelativeLayout rlDocuments;//证件类型
    @Bind(R.id.rl_business)
    RelativeLayout rlBusiness;//营业号码
    @Bind(R.id.rl_photo)
    RelativeLayout rlPhoto;//证件图片
    @Bind(R.id.rl_address)
    RelativeLayout rlAddress;//注册地址
    @Bind(R.id.rl_qualification)
    RelativeLayout rlQualification;//资质证书

    @Bind(R.id.tv_certif_status)
    TextView tvCertifStatus;//认证状态
    @Bind(R.id.tv_contact)
    TextView tvContact;//联系人
    @Bind(R.id.tv_phone)
    TextView tvPhone;//电话号码
    @Bind(R.id.tv_documents)
    TextView tvDocuments;//证件类型
    @Bind(R.id.tv_certificate_number)
    TextView tvCertificateNumber;//证件号码
    @Bind(R.id.tv_address)
    TextView tvAddress;//地址
    @Bind(R.id.tv_company_name)
    TextView tvCompanyName;//公司名称
    @Bind(R.id.tv_numbering)
    TextView tvNumbering;//型号
    @Bind(R.id.tv_type)
    TextView tvType;//公司类型

    private ACache mCache;
    LoginBean ownerBean;
    ServiceLoginBean serviceBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_enterprise_information);
        ButterKnife.bind(this);

        mCache = ACache.get(this);

        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();

    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);

        /**判断权限、管理员权限可以编辑、其他不可编辑**/
        tvEdit.setVisibility(View.VISIBLE);
        tvEdit.setText("编辑");
        ivBack.setOnClickListener(this);
        tvEdit.setOnClickListener(this);
        tvTitle.setText("企业信息");

        rlCertification.setOnClickListener(this);
        rlCode.setOnClickListener(this);
        rlContact.setOnClickListener(this);
        rlPhone.setOnClickListener(this);
        rlDocuments.setOnClickListener(this);
        rlBusiness.setOnClickListener(this);
        rlPhoto.setOnClickListener(this);
        rlAddress.setOnClickListener(this);
        rlQualification.setOnClickListener(this);

        ownerBean = (LoginBean) mCache.getAsObject("loginBean");
        serviceBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
        if (ownerBean != null){
            if ("1".equals(ownerBean.getRows().getCompany().getType())) {//业主

                tvCompanyName.setText(ownerBean.getRows().getCompany().getCompanyName());
                tvNumbering.setText(ownerBean.getRows().getCompany().getVersionCode());
                tvType.setText("/"+"业主");
                //认证状态 0：未认证，1：待认证，2已认证，3未通过认证
                if ("0".equals(ownerBean.getRows().getCompany().getCheckFlag())) {
                    tvCertifStatus.setText("未认证");
                } else if ("1".equals(ownerBean.getRows().getCompany().getCheckFlag())) {
                    tvCertifStatus.setText("待认证");
                } else if ("2".equals(ownerBean.getRows().getCompany().getCheckFlag())) {
                    tvCertifStatus.setText("已认证");
                } else if ("3".equals(ownerBean.getRows().getCompany().getCheckFlag())) {
                    tvCertifStatus.setText("未通过认证");
                }

                tvContact.setText(ownerBean.getRows().getCompany().getRegisterName());//联系人

                tvPhone.setText(ownerBean.getRows().getCompany().getCustomerPhone());//电话

                if ("1".equals(ownerBean.getRows().getCompany().getCertificateType())){
                    tvDocuments.setText("营业执照");
                }else if ("2".equals(ownerBean.getRows().getCompany().getCertificateType())){
                    tvDocuments.setText("组织机构代码证");
                }
                tvCertificateNumber.setText(ownerBean.getRows().getCompany().getCertificateNumber());//证件号码
                tvAddress.setText(ownerBean.getRows().getCompany().getRegisterAddr());

            }
        }

        if (serviceBean != null){
            if ("2".equals(serviceBean.getRows().getCompany().getType())) {//服务商
                tvCompanyName.setText(serviceBean.getRows().getCompany().getCompanyName());
                tvNumbering.setText(serviceBean.getRows().getCompany().getVersionCode());
                tvType.setText("/"+"服务商");
                //认证状态 0：未认证，1：待认证，2已认证，3未通过认证
                if ("0".equals(serviceBean.getRows().getCompany().getCheckFlag())) {
                    tvCertifStatus.setText("未认证");
                } else if ("1".equals(serviceBean.getRows().getCompany().getCheckFlag())) {
                    tvCertifStatus.setText("待认证");
                } else if ("2".equals(serviceBean.getRows().getCompany().getCheckFlag())) {
                    tvCertifStatus.setText("已认证");
                } else if ("3".equals(serviceBean.getRows().getCompany().getCheckFlag())) {
                    tvCertifStatus.setText("未通过认证");
                }

                tvContact.setText(serviceBean.getRows().getCompany().getRegisterName());//联系人

                tvPhone.setText(serviceBean.getRows().getCompany().getCustomerPhone());//电话
                if ("1".equals(serviceBean.getRows().getCompany().getCertificateType())){
                    tvDocuments.setText("营业执照");
                }else if ("2".equals(serviceBean.getRows().getCompany().getCertificateType())){
                    tvDocuments.setText("组织机构代码证");
                }

                tvCertificateNumber.setText(serviceBean.getRows().getCompany().getCertificateNumber());//证件号码

                tvAddress.setText(serviceBean.getRows().getCompany().getRegisterAddr());
            }
        }


        //showDialog();

    }

    //显示dialog
    private void showDialog() {
        // 实例化布局文件
        //View view = View.inflate(getApplicationContext(), R.layout.enterprise_information_dialog_layout, null);

        View view = View.inflate(getApplicationContext(), R.layout.update_dialog_layout, null);

        // 实例化 Dialog
        final Dialog dialog = new Dialog(EnterpriseInformationActivity.this);

        // 获取窗口管理器
        Window dialogWindow = dialog.getWindow();
        // 隐藏标题栏
        dialogWindow.requestFeature(Window.FEATURE_NO_TITLE);
        Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);//取消按钮
        Button btnConfirm = (Button) view.findViewById(R.id.btn_confirm);//确认按钮
        // 设置显示的View
        dialogWindow.setContentView(view);
        // 设置显示的位置
        dialogWindow.setGravity(Gravity.CENTER);
        dialog.show();
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_edit:
                break;
            case R.id.rl_certification://企业认证
                break;
            case R.id.rl_code://企业二维码
                startActivity(new Intent(this, QRCodeActivity.class));
                break;
            case R.id.rl_contact://联系人
                break;
            case R.id.rl_phone://手机号
                break;
            case R.id.rl_documents://证件类型
                startActivity(new Intent(this, DocumentsTypeActivity.class));
                break;
            case R.id.rl_business://营业号码
                break;
            case R.id.rl_photo://证件图片
                break;
            case R.id.rl_address://注册地址
                startActivity(new Intent(this, RegisterAddressActivity.class));
                break;
            case R.id.rl_qualification://资质证书
                startActivity(new Intent(this, CertificateActivity.class));
                break;
            default:
                break;
        }
    }
}
